﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AddCourseForm.cs" company="HelloWorld">
//   BY黄媛媛
// </copyright>
// <summary>
//   Defines the SubmitHandle type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Form
{
    using System;
    using System.Windows.Forms;

    using WindowsFormsApp2.Service;

    /// <summary>
    /// The submit handle.
    /// </summary>
    public delegate void SubmitHandle();

    /// <summary>
    /// The add course form.
    /// </summary>
    public partial class AddCourseForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AddCourseForm"/> class.
        /// </summary>
        public AddCourseForm()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// The submit click.
        /// </summary>
        public event SubmitHandle SubmitClick;

        /// <summary>
        /// The ok button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OKBtn_Click(object sender, EventArgs e)
        {
            long courseId;
            string courseName;
            int weekdayInt, beginLessonInt, lastLessonInt;
            try
            {
                courseId = long.Parse(this.CourseIDTxt.Text);
                courseName = this.couseNameTxt.Text;
                weekdayInt = this.weekdayTxt.SelectedIndex + 1;
                beginLessonInt = int.Parse(this.beginLessonTxt.Text);
                lastLessonInt = (int)this.lastLessonNum.Value;

                if(beginLessonInt + lastLessonInt > 14)
                {
                    MessageBox.Show("超出课时范围，添加失败");
                    return;
                }
            }
            catch (FormatException)
            {
                MessageBox.Show(@"输入格式不正确 请重新输入");
                return;
            }

            new CourseDataManage().AddCourse(courseId, courseName, weekdayInt, beginLessonInt, lastLessonInt);
            this.SubmitClick?.Invoke();
            this.Close();
        }

        /// <summary>
        /// The cancel button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void CancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lastLessonNum_ValueChanged(object sender, EventArgs e)
        {

        }
    }
}
